﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Modelo;
using JuegoBomberman.Vistas;

namespace JuegoBomberman.Controladores
{
    public class BombitaControlador
    {
        Bombita jugador;
        BombitaVista vista;
        KeyboardState oldKeyboardState;
        private float timer = 0f;
        private float interval = 100f;

        public BombitaControlador(Bombita jugador, BombitaVista vista)
        {
            this.jugador = jugador;
            this.vista = vista;
        }

        public void Actualizarse(KeyboardState keyboardState, GameTime gameTime)
        {
            if (keyboardState.IsKeyUp(Keys.Up) && keyboardState.IsKeyUp(Keys.Down) && keyboardState.IsKeyUp(Keys.Right) && keyboardState.IsKeyUp(Keys.Left))
            {
                this.vista.setFrame(1);
            }

            if (keyboardState.IsKeyDown(Keys.Up))
            {
                jugador.mover(0, -1);
                animacionArriba(keyboardState, gameTime);
            }
            else if (keyboardState.IsKeyDown(Keys.Down))
            {
                jugador.mover(0, 1);
                animacionAbajo(keyboardState, gameTime);
            }
            else if (keyboardState.IsKeyDown(Keys.Right))
            {
                jugador.mover(1, 0);
                animacionDerecha(keyboardState, gameTime);
            }
            else if (keyboardState.IsKeyDown(Keys.Left))
            {
                jugador.mover(-1, 0);
                animacionIzquierda(keyboardState, gameTime);
            }
            else if (keyboardState.IsKeyDown(Keys.RightShift) && oldKeyboardState.IsKeyUp(Keys.RightShift))
            {
                jugador.plantarBomba();
            }
            oldKeyboardState = keyboardState;
        }

        private void animacionArriba(KeyboardState keyboardState, GameTime gameTime)
        { 
            if (keyboardState != oldKeyboardState)
            {
                this.vista.setFrame(1);
                this.vista.setAnimacion(3);
            }

            timer += (float)gameTime.ElapsedGameTime.TotalMilliseconds;

	        if (timer >= interval)
	        {
                int frameActual = this.vista.getFrame();

		        if (frameActual == 1)
		        {
                    frameActual = 2;
		        }
                else if (frameActual == 2)
                {
                    frameActual = 1;
                }
                else
                {
                    frameActual = 2;
                }
                this.vista.setFrame(frameActual);
		        timer = 0f;
	        }
        }


        private void animacionAbajo(KeyboardState keyboardState, GameTime gameTime)
        {
            if (keyboardState != oldKeyboardState)
            {
                this.vista.setFrame(1);
                this.vista.setAnimacion(0);
            }

            timer += (float)gameTime.ElapsedGameTime.TotalMilliseconds;

            if (timer > interval)
            {
                int frameActual = this.vista.getFrame();

                if (frameActual == 1)
                {
                    frameActual = 2;
                }
                else if (frameActual == 2)
                {
                    frameActual = 1;
                }
                else
                {
                    frameActual = 2;
                }
                this.vista.setFrame(frameActual);
                timer = 0f;
            }
        }

        private void animacionDerecha(KeyboardState keyboardState, GameTime gameTime)
        {
            if (keyboardState != oldKeyboardState)
            {
                this.vista.setFrame(1);
                this.vista.setAnimacion(2);
            }

            timer += (float)gameTime.ElapsedGameTime.TotalMilliseconds;

            if (timer > interval)
            {
                int frameActual = this.vista.getFrame();

                if (frameActual == 1)
                {
                    frameActual = 2;
                }
                else if (frameActual == 2)
                {
                    frameActual = 1;
                }
                else
                {
                    frameActual = 2;
                }
                this.vista.setFrame(frameActual);
                timer = 0f;
            }
        }

        private void animacionIzquierda(KeyboardState keyboardState, GameTime gameTime)
        {
            if (keyboardState != oldKeyboardState)
            {
                this.vista.setFrame(1);
                this.vista.setAnimacion(1);
            }

            timer += (float)gameTime.ElapsedGameTime.TotalMilliseconds;

            if (timer > interval)
            {
                int frameActual = this.vista.getFrame();

                if (frameActual == 1)
                {
                    frameActual = 2;
                }
                else if (frameActual == 2)
                {
                    frameActual = 1;
                }
                else
                {
                    frameActual = 2;
                }
                this.vista.setFrame(frameActual);
                timer = 0f;
            }
        }
    }
}
